package lion.framework.core.web.request;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lion.dev.lang.MapJ;

/**
 * 请求封装的类
 *
 * <pre>
 * time   2013 2013-4-24 上午11:32:18
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion
 */
public class RequestModel extends MapJ {

	private static final long serialVersionUID = 1849235467939305974L;

	private HttpServletRequest request;

	public RequestModel(HttpServletRequest request) {

		this.request = request;

		List<String> names = Collections.list(this.request.getAttributeNames());
		for (String name : names) {
			if (name.startsWith("javax.servlet")) {
				continue;
			} // skip servlet context default attribute
			super.put(name, this.request.getAttribute(name));
		}
	}

	@Override
	public Object put(String key, Object value) {

		request.setAttribute(key, value);

		return super.put(key, value);
	}

	@Override
	public void putAll(Map<? extends String, ? extends Object> param) {

		for (String key : param.keySet()) {
			request.setAttribute(key, param.get(key));
		}
		super.putAll(param);
	}

	@Override
	public Object remove(Object key) {

		request.removeAttribute(String.valueOf(key));
		return super.remove(key);
	}

	@Override
	public void clear() {

		for (String key : keySet()) {
			request.removeAttribute(key);
		}
		super.clear();
	}
}
